package com.xwzn.resident;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xwzn.resident.fragment.FourFragment;
import com.xwzn.resident.fragment.OneFragment;
import com.xwzn.resident.fragment.ThreeFragment;
import com.xwzn.resident.fragment.TwoFragment;

/**
 * @Description 主界面
 * @Author HanKaiYang
 * @CreateDate 2019/12/23 11:05
 * @Version 1.0
 */
public class MainActivity extends AppCompatActivity {

    private FrameLayout fragment;
    private BottomNavigationView bottomNavigation;
    private Fragment[] fragments;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    private int lastfragment = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {

        fragment = (findViewById(R.id.fragment));
        bottomNavigation = findViewById(R.id.bottom_navigation);
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();
        fragments = new Fragment[]{oneFragment,twoFragment,threeFragment,fourFragment};
        //设置fragment到布局
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment,oneFragment)
                .show(oneFragment).commit();

        //给bottomNavigation设置图标选中效果为空，在selected里面设置
        bottomNavigation.setItemIconTintList(null);
        bottomNavigation.setItemTextAppearanceActive(R.style.bottom_normal_text);
        bottomNavigation.setItemTextAppearanceInactive(R.style.bottom_normal_text);
        //给bottomNavigation设置点击事件
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    /**
     * bottomNavigation的点击事件
    */
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.tab_one:
                    if (lastfragment != 0){
                        switchFragment(lastfragment,0);
                        lastfragment = 0;
                    }
                    return true;
                case R.id.tab_two:
                    if (lastfragment != 1){
                        switchFragment(lastfragment,1);
                        lastfragment = 1;
                    }
                    return true;
                case R.id.tab_three:
                    if (lastfragment != 2){
                        switchFragment(lastfragment,2);
                        lastfragment = 2;
                    }
                    return true;
                case R.id.tab_four:
                    if (lastfragment != 3){
                        switchFragment(lastfragment,3);
                        lastfragment = 3;
                    }
                    return true;
                default:
                    break;
            }
            return false;
        }
    };


    /**
     * 切换fragment
     * @param lastfragment
     * @param index
     */
    private void switchFragment(int lastfragment,int index){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //隐藏上个Fragment
        transaction.hide(fragments[lastfragment]);
        if (fragments[index].isAdded() == false){
            transaction.add(R.id.fragment,fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

}
